<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="menuLeft">
      </template>
      <!-- <template slot-scope="{type,size,row,index}" slot="menu">
        <el-button icon="el-icon-start" :size="size" :type="type" @click="huifu(row,index)">回复</el-button>
      </template> -->
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/service/xlpcdialogue";
  import {mapGetters} from "vuex";
  import {getXsTree} from "@/api/service/org";

  export default {
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        showHf:false,
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          menu:false,
          column: [
            {
              label: "主键",
              prop: "id",
              rules: [{
                required: true,
                message: "请输入主键",
                trigger: "blur"
              }],
              hide:true,
              display:false
            },
            // {
            //   label: "学生",
            //   prop: "xs_name",
            //   search:true,
            //   overHidden:true,
            //   rules: [{
            //     required: true,
            //     message: "请输入学生ID",
            //     trigger: "blur"
            //   }],
            //   type: "tree",
            //   dicData: [],
            //   props: {
            //     label: "title"
            //   },
            //   slot: true,
            //   checkStrictly: true,
            //   editDisabled:true,
            //   nodeClick:(data, node)=>{
            //     this.form.schoolId = node.parent.parent.parent.data.id;
            //     this.form.xsNickName = node.data.title;
            //   },
            // },
            // {
            //   label: "学校名称",
            //   prop: "schoolId",
            //   type: "select",
            //   search:false,
            //   overHidden:true,
            //   rules: [{
            //     required: true,
            //     message: "请输入学校名称",
            //     trigger: "blur"
            //   }],
            //   dicUrl: "/api/school-service/school/select",
            //   props: {
            //     label: "xxName",
            //     value: "id"
            //   },
            //   addDisabled:true,
            //   editDisabled:true,
            //   hide:true,
            // },
            {
              label: "学生昵称",
              prop: "xs_name",
              overHidden:true,
              rules: [{
                required: false,
                message: "请输入学生昵称",
                trigger: "blur"
              }],
              maxlength:20,
              showWordLimit:true,
              addDisabled: true,
              editDisabled: true,
            },
            {
              label: "教师昵称",
              prop: "js_name",
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入教师昵称",
                trigger: "blur"
              }],
              maxlength:20,
              showWordLimit:true
            },
            {
              label: "次数",
              prop: "count",
              type: "textarea",
              span: 24,
              minRows: 6,
              rules: [{
                required: true,
                message: "请输入内容",
                trigger: "blur"
              }],
              hide: false,
              overHidden: true,
              maxlength:200,
              showWordLimit:true
            },
            // {
            //   label: "内容时间",
            //   prop: "contentTime",
            //   type: "datetime",
            //   format: "yyyy-MM-dd HH:mm:ss",
            //   valueFormat: "yyyy-MM-dd HH:mm:ss",
            //   overHidden:true,
            //   rules: [{
            //     required: true,
            //     message: "请输入内容时间",
            //     trigger: "blur"
            //   }]
            // },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: false,
          viewBtn: false,
          delBtn: false,
          editBtn:false
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      // huifu(row,index){
      //   this.$refs.crud.rowAdd();
      //   this.form.xsId = row.xsId;
      //   this.form.schoolId = row.schoolId;
      // },
      rowSave(row, done, loading) {
        add(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.totalItems;
          this.data = data.items;
          this.loading = false;
          this.selectionClear();
        });
        // const school = this.findObject(this.option.column, "schoolId");
        // school.hide = true;

        getXsTree(this.form.tenantId).then(res => {
          const column = this.findObject(this.option.column, "xsId");
          column.dicData = res.data.data;
        });
        this.form.contentTime = new Date();
      }
    }
  };
</script>

<style>
</style>
